How to Solve Image captcha with Python

Ethan Collins
Pattern Recognition Specialist
21-Sep-2023


Introduction
Image-to-Text captchas are one of the most common forms of bot verification on the internet. They present an image containing distorted, warped, or obscured characters and require the user to correctly identify and enter the text to prove they are human. For developers working on web scraping or automation, these captchas are a widespread obstacle.
Fortunately, with AI-powered recognition services, this process can be easily automated. This guide will show you how to use Python and the CapSolver service to solve image captchas quickly and accurately.
⚙️ Prerequisites
Before you begin, make sure you have the following:
- Python installed: Ensure Python is set up on your system.
- CapSolver API Key: You will need a CapSolver account to get your API key from the dashboard.
🤖 Step 1: Install Necessary Python Packages
First, you need to install the official CapSolver Python library. This package simplifies interaction with the CapSolver API. Open your terminal and run the following command:
bash
pip install capsolver
👨💻 Step 2: Write Python Code to Solve Image Captcha
The following Python script demonstrates how to solve an image captcha using CapSolver. The core of this code is the ImageToTextTask, which leverages CapSolver's powerful Vision Engine to recognize the text within an image.
Updated Python Code
This code is more modular and includes functions to handle both local image files and image URLs, making it more versatile for different use cases.
python
import capsolver
import base64
import requests
from io import BytesIO
# -------------------PLEASE MODIFY THESE VALUES-------------------
# Your API key from the CapSolver Dashboard
CAPSOLVER_API_KEY = "Your_API_KEY"
# ----------------------------------------------------------------
def setup_capsolver():
"""Sets the CapSolver API key."""
capsolver.api_key = CAPSOLVER_API_KEY
def get_base64_from_image(source, is_url=False):
"""
Converts an image file or URL into a Base64 encoded string.
:param source: The path (for a local file) or URL of the image.
:param is_url: True if the source is a URL.
:return: A Base64 encoded string, or None on failure.
"""
try:
if is_url:
response = requests.get(source)
response.raise_for_status() # Raise an exception for bad status codes
image_bytes = BytesIO(response.content)
else:
with open(source, 'rb') as image_file:
image_bytes = BytesIO(image_file.read())
base64_string = base64.b64encode(image_bytes.read()).decode('utf-8')
return base64_string
except requests.exceptions.RequestException as e:
print(f"Could not fetch image from URL: {e}")
return None
except FileNotFoundError:
print(f"Error: File not found at {source}")
return None
except Exception as e:
print(f"An error occurred converting image to Base64: {e}")
return None
def solve_image_captcha(base64_image, module_name="common"):
"""
Solves an image captcha using CapSolver.
:param base64_image: The Base64 encoded image string.
:param module_name: The recognition module, 'common' for general purpose.
:return: The solution object, or None on failure.
"""
print("Submitting image captcha task to CapSolver...")
try:
solution = capsolver.solve({
"type": "ImageToTextTask",
"module": module_name,
"body": base64_image
})
return solution
except Exception as e:
print(f"An error occurred while solving the captcha: {e}")
return None
def main():
"""Main function to execute the process."""
setup_capsolver()
# --- Example 1: Using a local image file ---
print("--- Example 1: Using a local image file ---")
local_image_path = "path/to/your/captcha.jpg" # Replace with your image path
base64_from_file = get_base64_from_image(local_image_path)
if base64_from_file:
solution = solve_image_captcha(base64_from_file)
if solution:
print("Solution Text:", solution.get("text"))
print("\n" + "="*30 + "\n")
# --- Example 2: Using an image URL ---
print("--- Example 2: Using an image URL ---")
image_url = "https://i.postimg.cc/B6hK2V19/captcha-example.png" # Example image URL
base64_from_url = get_base64_from_image(image_url, is_url=True)
if base64_from_url:
solution = solve_image_captcha(base64_from_url)
if solution:
print("Solution Text:", solution.get("text"))
if __name__ == "__main__":
main()
⚠️ Important Variables to Change
Before running the code, be sure to modify the following:
CAPSOLVER_API_KEY: Find your API key in the CapSolver Dashboard and replace the placeholder.local_image_path(in themainfunction): If you want to test a local image, replace this with the actual file path to your captcha image.
Conclusion
By integrating CapSolver into your Python scripts, you can fully automate the process of recognizing image captchas. This method is not only efficient and accurate but also frees you from the repetitive manual labor of solving these challenges. It's a critical efficiency boost for any project involving large-scale data collection or automated testing. If you're interested in tackling more complex captcha types, you can learn how to solve reCAPTCHA v2 with Playwright, which demonstrates handling dynamic challenges in a browser automation context.
Frequently Asked Questions (FAQ)
Q1: How accurate is CapSolver's recognition?
A1: CapSolver uses advanced AI models and achieves a high accuracy rate for most standard image captchas. The accuracy can vary depending on the complexity of the captcha (e.g., level of distortion, background noise).
Q2: What is the module parameter for?
A2: The module parameter allows you to specify a particular recognition engine to optimize performance for a certain type of captcha. common is the general-purpose module suitable for most cases. For more specialized modules, refer to the official ImageToTextTask documentation.
Q3: Can I use this method directly in my web scraper?
A3: Absolutely. Once you have the recognized text, you can use it to fill out the captcha form and proceed with your scraping or automation workflow. This method can be seamlessly integrated into any Python project.
Q4: Does CapSolver support languages other than Python?
A4: Yes. CapSolver provides libraries and API endpoints for multiple programming languages, including Node.js, Go, and others, making it easy to integrate into virtually any tech stack.
Compliance Disclaimer: The information provided on this blog is for informational purposes only. CapSolver is committed to compliance with all applicable laws and regulations. The use of the CapSolver network for illegal, fraudulent, or abusive activities is strictly prohibited and will be investigated. Our captcha-solving solutions enhance user experience while ensuring 100% compliance in helping solve captcha difficulties during public data crawling. We encourage responsible use of our services. For more information, please visit our Terms of Service and Privacy Policy.
More

Image Recognition API for Custom CAPTCHAs: How It Works in Automation
Discover how an Image Recognition API for custom CAPTCHAs streamlines automation. Learn about AI vision logic, OCR vs. AI, and CapSolver's modular solutions.

Rajinder Singh
03-Apr-2026

CAPTCHA Solving API Response Time Explained: Speed & Performance Factors
Understand CAPTCHA solving API response time, its impact on automation, and key factors affecting speed. Learn how to optimize performance and leverage efficient solutions like CapSolver for rapid CAPTCHA resolution.

Emma Foster
03-Apr-2026

What Is a CAPTCHA Solving API? How It Works and When to Use It
Learn what a CAPTCHA solving API is, how it works, and when to use it for automation. Discover the benefits of AI-powered CAPTCHA resolution for web scraping.

Sora Fujimoto
02-Apr-2026

Why CAPTCHA Blocks Users: Triggers, Avoidance & Solutions
Explore why CAPTCHA blocks legitimate users, common triggers like bad IP reputation and browser issues, and effective avoidance strategies. Learn how professional solutions like CapSolver handle CAPTCHA at scale for automation.

Nikolai Smirnov
02-Apr-2026

Mastering CAPTCHA Challenges in Job Data Scraping (2026 Guide)
A comprehensive guide to understanding and overcoming the CAPTCHA challenge in job data scraping. Learn to handle reCAPTCHA and other hurdles with our expert tips and code examples.

Sora Fujimoto
27-Feb-2026

Top 10 Data Collection Methods for AI and Machine Learning
Discover the 10 best data collection methods for AI and ML, focusing on Throughput, Cost, and Scalability. Learn how CapSolver's AI-powered captcha solving ensures stable data acquisition for your projects.

Sora Fujimoto
22-Dec-2025

